/*
 * DataHolder.java
 * 
 * last update: 16.01.2010 by Stefan Saru
 * 
 * author:	Alec(panovici@elcom.pub.ro)
 * 
 * Obs:
 */


package engine;

import java.util.*;

/**
 * Implements a "thing" witch can hold
 * a value whose change can be monitorized
 */

@SuppressWarnings("serial")
public abstract class DataHolder extends Assignable implements Result{

	/**
	 *  the list of monitors
	 */
	Vector monitors;

	public DataHolder(){
		monitors = new Vector();
	}

	final void addMonitor(Monitor m){
		monitors.addElement(m);
	}

	final int hasMonitor(Monitor m){
		return monitors.lastIndexOf(m);
	}

	final boolean removeMonitor(Monitor m){
		return monitors.removeElement(m);
	}

	/**
	 * Returns the watch monitor for this dataholder ( or null);
	 */
	final Monitor getWatchMonitor(){
		//since the watch monitor is created at the construction time of each dataholder, 
		//it must be monitor[0]
		if(monitors != null)return (Monitor) monitors.elementAt(0);
		return null;
	}

	/**
	 *  each DataHolder should call this method 
	 *  after his data has changed
	 */
	void notifyMonitors()
	throws InterpretTimeException
	{
		//    xConsole.debug("triggering...");
		for(Enumeration e = monitors.elements() ; e.hasMoreElements() ; )
			((Monitor)e.nextElement()).trigger();
	}

}
